package com.sbs.session.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.session.data.redis.config.annotation.web.http.EnableRedisHttpSession;

import java.net.UnknownHostException;

/**
 * @Author fyy
 * @Description Redis配置类
 * @Date 2020/5/18 11:15
 */
@Configuration
@EnableRedisHttpSession(maxInactiveIntervalInSeconds = 86400 * 30)
public class RedisConfig {

    /**
     * @Author fyy
     * @Description 序列化
     * @Date 2020/5/18 11:16
     */
    @Bean(name = "genericJackson2JsonRedisSerializer")
    public GenericJackson2JsonRedisSerializer genericJackson2JsonRedisSerializer(){
        return new GenericJackson2JsonRedisSerializer();
    }

    /**
     * @Author fyy
     * @Description redis模板配置
     * @Date 2020/5/18 11:18
     */
    @Bean(name = "redisTemplate")
    public RedisTemplate<Object,Object> myRedisTemplate(
            RedisConnectionFactory redisConnectionFactory, GenericJackson2JsonRedisSerializer ser)
            throws UnknownHostException {
        RedisTemplate<Object,Object> template = new RedisTemplate<>();
        template.setConnectionFactory(redisConnectionFactory);
        template.setDefaultSerializer(ser); //设置序列化方式
        return template;
    }

}
